﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.Entity.Infrastructure;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CubicFWNet.Application.SystemManager;
using CubicFWNet.Common;
using CubicFWNet.Data.Models;
using CubicFWNet.Data.ViewModal;
using PagedList;

namespace CubicFWNet.Mvc.Controllers
{
    public class MaterialController : ControllerBase
    {
        private ProductApp _productApp;
        private MaterialFormatApp _materialFormatAppApp;

        public MaterialController()
        {
            _productApp = new ProductApp();
            _materialFormatAppApp = new MaterialFormatApp();
        }



        /// <summary>
        /// 主料查询
        /// </summary>
        /// <returns></returns>
        public ActionResult Index(int page = 1, string productName = "", string productNum = "")
        {
            ViewBag.ProductName = productName;
            ViewBag.ProductNum = productNum;
            IPagedList<ProductView> productList = _productApp.GetMaterialList(2, 0, productName, productNum).ToPagedList(page, 10);
            return View(productList);
        }

        /// <summary>
        /// 主料添加
        /// </summary>
        /// <returns></returns>
        public ActionResult MaterialAdd()
        {
            ViewBag.RealName = operatorModel.FamilyName;
            return View();
        }

        /// <summary>
        /// 主料添加[提交表单]
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult MaterialAdd(FormCollection fc)
        {
            Product pro = new Product();
            pro.Id = Guid.NewGuid();
            pro.Name = fc["ProductName"];
            pro.Number = fc["Number"];
            pro.Format = fc["Format"];
            pro.Memo = fc["Memo"];
            pro.Unit = fc["Unit"];

            //pro.Explain = fc["Explain"];
            //pro.Pack = fc["Pack"];
            //pro.Data = fc["Data"];
            //pro.Color = fc["Color"];

            //pro.Type = 2;
            pro.Price = Convert.ToDecimal(fc["Price"]);
            pro.SizeWidth = fc["Width"];
            pro.SizeLenth = fc["Length"];
            pro.CreateTime = DateTime.Now;
            pro.CreatorId = operatorModel.UserId;
            pro.CreatorName = operatorModel.FamilyName;
            pro.Status = 1;

            int i = _productApp.Insert(pro);
            if (i == 1)
            {
                MaterialFormat mf = new MaterialFormat();
                mf.Id = Guid.NewGuid();
                mf.MaterialId = pro.Id;
                mf.Width = pro.SizeWidth;
                mf.Length = pro.SizeLenth;
                mf.Format = pro.SizeWidth + "*" + pro.SizeLenth;
                int res = _materialFormatAppApp.Add(mf);

                return RedirectToAction("Index", "Material");
            }
            else
            {
                ViewBag.Info = "抱歉，添加失败！";
            }

            ViewBag.RealName = operatorModel.FamilyName;
            return View();
        }

        /// <summary>
        /// 辅料查询
        /// </summary>
        /// <returns></returns>
        public ActionResult Accessory(int page = 1, string productName = "", string productNum = "")
        {
            ViewBag.ProductName = productName;
            ViewBag.ProductNum = productNum;

            IPagedList<ProductView> productList = _productApp.GetMaterialList(3, 0, productName, productNum).ToPagedList(page, 10);
            return View(productList);
        }

        /// <summary>
        /// 辅料添加
        /// </summary>
        /// <returns></returns>
        public ActionResult AccessoryAdd()
        {
            ViewBag.RealName = operatorModel.FamilyName;
            return View();
        }

        /// <summary>
        /// 辅料添加[提交表单]
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AccessoryAdd(FormCollection fc)
        {
            Product pro = new Product();
            pro.Id = Guid.NewGuid();
            pro.Name = fc["ProductName"];
            pro.Number = fc["Number"];
            pro.Format = fc["Format"];
            pro.Memo = fc["Memo"];
            pro.Unit = fc["Unit"];

            //pro.Explain = fc["Explain"];
            //pro.Pack = fc["Pack"];
            //pro.Data = fc["Data"];
            //pro.Color = fc["Color"];

            //pro.Type = 3;
            pro.Price = Convert.ToDecimal(fc["Price"]);
            pro.SizeWidth = fc["Width"];
            pro.SizeLenth = fc["Length"];
            pro.CreateTime = DateTime.Now;
            pro.CreatorId = operatorModel.UserId;
            pro.CreatorName = operatorModel.FamilyName;
            pro.Status = 1;

            int i = _productApp.Insert(pro);
            if (i == 1)
            {
                return RedirectToAction("Accessory", "Material");
            }
            else
            {
                ViewBag.Info = "抱歉，添加失败！";
            }

            ViewBag.RealName = operatorModel.FamilyName;
            return View();
        }

        /// <summary>
        /// 原料编辑
        /// </summary>
        /// <returns></returns>
        public ActionResult MaterialEdit()
        {
            string pid = Request.QueryString["PID"];
            Guid id = new Guid(pid);
            Product pro = _productApp.GetInfo(id);
            if (pro != null)
            {
                ViewBag.Product = pro;
            }
            ViewBag.RealName = pro.CreatorName;
            return View();
        }

        /// <summary>
        /// 原料编辑[提交表单]
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult MaterialEdit(FormCollection fc)
        {
            string pid = fc["ProductID"];
            Guid id = new Guid(pid);
            Product pro = _productApp.GetInfo(id);
            if (null != pro)
            {
                pro.Name = fc["ProductName"];
                pro.Number = fc["Number"];
                pro.Format = fc["Format"];
                pro.Memo = fc["Memo"];
                pro.Unit = fc["Unit"];

                //pro.Explain = fc["Explain"];
                //pro.Pack = fc["Pack"];
                //pro.Data = fc["Data"];
                //pro.Color = fc["Color"];

                pro.Price = Convert.ToDecimal(fc["Price"]);
                pro.SizeWidth = fc["Width"];
                pro.SizeLenth = fc["Length"];
                // pro.Status = Convert.ToInt32(fc["Status"]);

                int i = _productApp.Update(pro);
                if (i == 1)
                {
                    //if (pro.Type == 3)
                    //{
                    //    return RedirectToAction("Accessory", "Material");
                    //}
                    //else
                    //{
                    return RedirectToAction("Index", "Material");
                    //}
                }
                else
                {
                    ViewBag.Info = "抱歉，操作失败！";
                }
            }
            ViewBag.RealName = pro.CreatorName;
            return View();
        }

        /// <summary>
        /// 原料规格
        /// </summary>
        /// <returns></returns>
        public ActionResult MaterialFormat()
        {
            string id = Request.QueryString["PID"];
            ViewBag.PID = id;
            IEnumerable<MaterialFormat> ls = _materialFormatAppApp.GetListByPID(new Guid(id));
            return View(ls);
        }

        /// <summary>
        /// 原料规格【提交】
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult MaterialFormat(FormCollection fc)
        {
            string id = fc["PID"];
            Guid pID = new Guid(id);
            string width = fc["Width"];
            string length = fc["Length"];
            string memo = fc["Memo"];
            string format = width.ToString() + " * " + length.ToString();

            MaterialFormat mf = new MaterialFormat();
            mf.Id = Guid.NewGuid();
            mf.MaterialId = pID;
            mf.Width = width;
            mf.Length = length;
            mf.Memo = memo;
            mf.Format = format;

            int res = _materialFormatAppApp.Add(mf);
            if (res == -1)
            {
                ViewBag.Info = "已存在该规格，请重新输入！";
            }
            ViewBag.PID = pID;
            IEnumerable<MaterialFormat> ls = _materialFormatAppApp.GetListByPID(new Guid(id));
            return View(ls);
        }

        /// <summary>
        /// 更新产品状态
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public JsonResult ProChange(string id)
        {
            Guid pid = new Guid(id);
            Product pro = _productApp.GetInfo(pid);
            if (pro.Status == -2)
            {
                pro.Status = 1;
            }
            else if (pro.Status == 1)
            {
                pro.Status = -2;
            }
            int res = _productApp.Update(pro);
            return Json(res);
        }

    }
}